package shin.test.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import shin.test.web.logic.Greeting;
import shin.test.web.logic.Target;
import shin.test.web.service.IGreetingOpration;

@Controller
@RequestMapping(value = "/test")  
public class HelloController {

	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired
	private Target target;
	
	@Autowired  
	@Qualifier("testGreetingOpration")
	private IGreetingOpration iGreetingOpration;
	

	public IGreetingOpration getiGreetingOpration() {
		return iGreetingOpration;
	}

	public void setiGreetingOpration(IGreetingOpration iGreetingOpration) {
		this.iGreetingOpration = iGreetingOpration;
	}

	public void setTarget(Target target) {
		this.target = target;
	}

	public Target getTarget() {
		return target;
	}

	@RequestMapping(value = "/ioc", method = RequestMethod.GET)  
	public String ioc(ModelMap model){
		target.setMessage("Hello World, Spring 3.0!");
		model.addAttribute("msg", target.getMessage());
		return "/view/test/ioc.jsp";
	}
	
	@RequestMapping(value = "/mvc", method = RequestMethod.GET)  
	public String mvc(HttpServletRequest request, ModelMap model) {
		String message = "Hello World, Spring 3.0!";
		List<Greeting> greetings = iGreetingOpration.gets();
		model.addAttribute("msg", message);
		model.addAttribute("greetings", greetings.isEmpty()?null:greetings);
		return "/view/test/mvc.jsp";
	}

	@RequestMapping(value = "/sign", method = RequestMethod.POST)
	public String sign(HttpServletRequest request, ModelMap model) {
		logger.info("/sign sign:"+ request.getParameter("content"));
		String content = request.getParameter("content");
		iGreetingOpration.save(content);
		return "redirect:/test/mvc.htm";
	}
}
